package com.example.malllocationserver.controller;
import com.example.malllocationserver.entity.History;
import com.example.malllocationserver.repository.StoreData;
import com.example.malllocationserver.repository.UserRepository;

import com.example.malllocationserver.entity.User;
import com.example.malllocationserver.utils.CreateGraph;
import com.example.malllocationserver.utils.location;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.malllocationserver.entity.data.Data;
import com.example.malllocationserver.utils.GetToken;

import java.util.*;

@RestController
public class TestController {
    @Autowired
    private StoreData storeData;
    @Autowired
    private UserRepository userRepository;
    //    private JdbcTemplate jdbcTemplate;
    private location knn;
    private GetToken Token = new GetToken();
    @RequestMapping(value="/getUsers",method = RequestMethod.GET)
    @ResponseBody
    public List<User> findAll(){
        List<User> userList = userRepository.findAll();
        return  userList;
    }
    @RequestMapping(value = "/register",method = RequestMethod.GET)
    public boolean register(@RequestParam("userid") String userId,
                            @RequestParam("username") String userName,
                            @RequestParam("password") String password,
                            @RequestParam("phone") String phone,
                            @RequestParam("sex") String sex,
                            @RequestParam("reg_time") String reg_time){
        User user = null;
        user = userRepository.login(userName,password);
        if(user != null) return false;
        else {
            java.sql.Timestamp date = Token.strToDate(reg_time);
            userRepository.register(userId,userName,password,phone,sex,date);
            System.out.println("可以正常执行");
            return true; //注册成功则返回true
        }
//        try {
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }return false;
    }
    @RequestMapping(value ="/login",method = RequestMethod.GET)
    public User login(@RequestParam("userId") String userId,
                         @RequestParam("password") String password){
        User user=null;
        user = userRepository.login(userId,password);
        //对比密码，相同则返回true，正常情况会加密解密，为求简单就不那么做了
        if(user == null) return null;
        if(user.getPassword().equals(password))
            return user;
        return null;
    }

    @RequestMapping(value = "/changePhone",method = RequestMethod.POST)
    public boolean changePhone(@RequestParam("phone") String phone,
                               @RequestParam("username") String username){
        User user = null;
        user = userRepository.find(username);
        if(user == null)
            return false;
        else{
            return userRepository.updatePhone(username,phone);
        }
    }

    @RequestMapping(value = "/changePassword",method = RequestMethod.POST)
    public boolean changePassword(@RequestParam("oldPassword") String old_password,
                                  @RequestParam("newPassword") String new_password,
                                  @RequestParam("username") String username){
        User user = null;
        user = userRepository.login(username,old_password);

        if(user == null)
            return false;                               //当前用户名或密码不正确
        else if(!user.getPassword().equals(old_password)){
            return false;
        }
        else{
            return userRepository.updatePassword(username,new_password);        //修改成功
        }
    }

    @RequestMapping(value = "/forgetPassword",method = RequestMethod.POST)
    public int changePassword(@RequestParam("username") String userid,
                                  @RequestParam("phone") String phone,
                                  @RequestParam("sex") String sex,
                                  @RequestParam("password") String password){
        User user = null;
        user = userRepository.find(userid);
        if(user == null) return 0;                      //用户不存在
        else if(!user.getSex().equals(sex)) return -1;           //性别不正确
        else if(!user.getPhone().equals(phone)) return -2;       //电话号码不正确
        else if(userRepository.updatePassword(userid,password)){
            return 1;                                           //修改成功
        }else{
            return 0;
        }
    }

    @RequestMapping(value = "/load",method = RequestMethod.POST)
    public boolean loadData(
                            @RequestParam("serial" ) String serial,
                            @RequestParam("position") String position,
                            @RequestParam("wifi") String wifi,
                            @RequestParam("global") String global,
                            @RequestParam("load") String load){
        if(storeData.storeData(serial,position,wifi,global,load)) return true;
        return false;
    }

    @RequestMapping(value = "/position",method = RequestMethod.GET)
    public String[] position(
            @RequestParam("wifi") String wifi,
            @RequestParam("load") String load){
        List<Data> data = storeData.position();
        knn = new location(data,wifi,load);
        String[] temp = knn.getLocation();
        return temp;
    }

    @RequestMapping(value = "/history",method = RequestMethod.GET)
    public List<History> find_history(
            @RequestParam("userid") String userid){
        List<History> list = new ArrayList<>();
        list = userRepository.find_history(userid);
        return list;
    }

    @RequestMapping(value = "/history/delete",method = RequestMethod.POST)
    public boolean delete_history(
            @RequestParam("userid") String userid){
        if(userRepository.delete_history(userid)) return true;
        else return false;
    }

    @RequestMapping(value = "/getToken",method = RequestMethod.GET)
    public String getToken(
            @RequestParam("userid") String userid){
        User user = userRepository.find(userid);
        String token = "";
        if(user != null)
        {
            token = Token.getToken();
        }
        return token;
    }

    @RequestMapping(value = "/addHistory",method = RequestMethod.POST)
    public boolean addHistory(
            @RequestParam("userid") String userid,
            @RequestParam("storename") String storename,
            @RequestParam("time") String time){
        java.sql.Timestamp date = Token.strToDate(time);
        if(userRepository.addHistory(userid,storename,date)){
            return true;
        }
        return false;
    }

    @RequestMapping(value = "/coordinate",method = RequestMethod.GET)
    public Map<String, String> coordinate(
            @RequestParam("wifi") String wifi,
            @RequestParam("load") String load){
        List<Data> data = storeData.position();
        knn = new location(data,wifi,load);
        float[] temp = knn.getCoordinate();
        if(temp.length == 3)
        {
            Map<String,String> map = new HashMap<>();
            map.put("x",String.valueOf(temp[0]));
            map.put("y",String.valueOf(temp[1]));
            map.put("zl", String.valueOf(temp[2]));
            return map;
        } else return null;
    }

    @RequestMapping(value = "/profile",method = RequestMethod.GET)
    public User getUser(
            @RequestParam("userid") String userid){
        return userRepository.getUser(userid);
    }

    @RequestMapping(value = "savePic",method = RequestMethod.POST)
    public boolean save(
            @RequestParam("userid") String userid,
            @RequestParam("picture") String picture){
        return userRepository.save(userid,picture);
    }

    @RequestMapping(value = "/navigation",method = RequestMethod.GET)
    public List<Float[]> Navigation(
            @RequestParam("floor") int floor,
            @RequestParam("origin") int origin,
            @RequestParam("destination") int destination){
        CreateGraph.startShortestDistance(floor,origin,destination);
        return CreateGraph.getList();
    }
}


